﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using Zero.UControl.Component.Common;

namespace Zero.UControl.Component
{
    /// <summary>
    /// ImageToolBox.xaml 的交互逻辑
    /// </summary>
    public partial class ImageToolBox : UserControl
    {
        public ImageToolBox()
        {
            InitializeComponent();
        }


        public static readonly RoutedEvent DeleteEvent = EventManager.RegisterRoutedEvent(
            nameof(Delete),
            RoutingStrategy.Bubble,
            typeof(RoutedEventHandler),
            typeof(ImageToolBox));

        public event RoutedEventHandler Delete
        {
            add => AddHandler(DeleteEvent, value);
            remove => RemoveHandler(DeleteEvent, value);
        }


        #region 依赖属性
        public BitmapImage UrlSource
        {
            get { return (BitmapImage)GetValue(UrlSourceProperty); }
            set { SetValue(UrlSourceProperty, value); }
        }

        public static readonly DependencyProperty UrlSourceProperty =
            DependencyProperty.Register("UrlSource", typeof(BitmapImage), typeof(ImageToolBox), new PropertyMetadata(null));

        public bool AllowEnlarge
        {
            get { return (bool)GetValue(AllowEnlargeProperty); }
            set { SetValue(AllowEnlargeProperty, value); }
        }

        public static readonly DependencyProperty AllowEnlargeProperty =
            DependencyProperty.Register("AllowEnlarge", typeof(bool), typeof(ImageToolBox), new PropertyMetadata(true, OnPropertyChanged));

        public bool AllowDownload
        {
            get { return (bool)GetValue(AllowDownloadProperty); }
            set { SetValue(AllowDownloadProperty, value); }
        }

        public static readonly DependencyProperty AllowDownloadProperty =
            DependencyProperty.Register("AllowDownload", typeof(bool), typeof(ImageToolBox), new PropertyMetadata(true, OnPropertyChanged));

        public bool AllowDelete
        {
            get { return (bool)GetValue(AllowDeleteProperty); }
            set { SetValue(AllowDeleteProperty, value); }
        }

        public static readonly DependencyProperty AllowDeleteProperty =
            DependencyProperty.Register("AllowDelete", typeof(bool), typeof(ImageToolBox), new PropertyMetadata(true, OnPropertyChanged));


        private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var ctrl = d as ImageToolBox;
            if (ctrl == null) return;

            string propName = e.Property.Name;
            bool flag = bool.Parse(e.NewValue.ToString());
            if (propName == "AllowEnlarge")
            {
                ctrl.enlarge.Visibility = flag ? Visibility.Visible : Visibility.Collapsed;
            }
            else if (propName == "AllowDownload")
            {
                ctrl.download.Visibility = flag ? Visibility.Visible : Visibility.Collapsed;
            }
            else if (propName == "AllowDelete")
            {
                ctrl.delete.Visibility = flag ? Visibility.Visible : Visibility.Collapsed;
            }
        }
        #endregion

        #region 本地事件
        private void Root_MouseEnter(object sender, MouseEventArgs e)
        {
            mask.Visibility = Visibility.Visible;
        }

        private void Root_MouseLeave(object sender, MouseEventArgs e)
        {
            mask.Visibility = Visibility.Collapsed;
        }

        private void enlarge_Click(object sender, RoutedEventArgs e)
        {
            string path = UrlSource.UriSource.LocalPath;
            new ImageWindow(path).Show();
        }

        private void delete_Click(object sender, RoutedEventArgs e)
        {
            RaiseEvent(new RoutedEventArgs(DeleteEvent, this));
        }
        #endregion

        private async void download_Click(object sender, RoutedEventArgs e)
        {
            string url = UrlSource.UriSource.AbsoluteUri;   // file:///E:/%E5%9B%BE%E7%89%87/wx.png

            string deskPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.InitialDirectory = deskPath;
            dialog.Filter = "(*.*)|*.*";
            string savePath = string.Empty;
            if (!dialog.ShowDialog().Value)
                return;

            savePath = dialog.FileName;
            if (url.StartsWith("http", StringComparison.OrdinalIgnoreCase) || url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                await HttpDownloader.DownloadFileAsync(url, dialog.FileName);
            }
            else if(url.StartsWith("file", StringComparison.OrdinalIgnoreCase))
            {
                string localPath = new Uri(url).LocalPath;
                File.Copy(localPath, savePath);
            }
        }
    }
}
